package s;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.Statement;



public class RequestRSS extends HttpServlet{

		private static final long serialVersionUID = 1L;
		private Properties props;
		private String propsPath;
	
		@Override
		public void init(ServletConfig config) throws ServletException {
			super.init(config);
			
			this.propsPath = this.getServletContext().getRealPath("/");
			this.props = new Properties();
			try {
				props.loadFromXML(new FileInputStream(propsPath + "properties.properties"));
			} catch (FileNotFoundException e) {e.printStackTrace();} 
			catch (IOException e) {e.printStackTrace();}
	}
	
	
		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
				String id = req.getParameter("sessionID");
				
				Connection connection = null;
				Statement statement = null;
				String rss = "";
				
				try {
					Class.forName("com.mysql.jdbc.Driver");
					connection = DriverManager.getConnection(props.getProperty("DB_URL"), props.getProperty("DB_USER"), props.getProperty("DB_PASS"));
						statement = (Statement) connection.createStatement();
						ResultSet rs = statement.executeQuery( "SELECT rss FROM sessions WHERE Id = '"+id+"';");
						while(rs.next()){
								rss = rs.getString("rss");
						}
				} catch (SQLException e) {e.printStackTrace();} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
				
				PrintWriter pw = res.getWriter();
				pw.print(rss);
		}	
}